package com.edu.neusoft.student.controller;

import com.edu.neusoft.student.domain.Customer;
import com.edu.neusoft.student.service.CustomerService;
import com.edu.neusoft.student.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/customers")
@Api(tags = "Customer Management API", description = "Operations related to customer management")
@Validated  // 启用参数校验
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    // 获取所有客户信息
    @GetMapping
    @ApiOperation(value = "Get all customers", notes = "Retrieve all customers in the system. This operation returns a list of all customers.")
    public AjaxResult getAllCustomers() {
        List<Customer> customers = customerService.selectAllCustomers();
        if (customers != null && !customers.isEmpty()) {
            return AjaxResult.success("Successfully retrieved all customers", customers);
        } else {
            return AjaxResult.error("No customers found");
        }
    }

    // 根据ID获取客户信息
    @GetMapping("/{customerid}")
    @ApiOperation(value = "Get customer by ID", notes = "Retrieve a customer by their unique customer ID. Provide the customer ID to fetch the customer details.")
    public AjaxResult getCustomerById(@PathVariable Integer customerid) {
        Customer customer = customerService.selectByPrimaryKey(customerid);
        if (customer != null) {
            return AjaxResult.success("Customer found", customer);
        } else {
            return AjaxResult.error("Customer not found");
        }
    }

    // 添加新客户
    @PostMapping
    @ApiOperation(value = "Add new customer", notes = "Add a new customer to the system. Provide customer details in the request body to create a new record.")
    public AjaxResult addCustomer(@Valid @RequestBody Customer customer) {
        int result = customerService.insert(customer);
        if (result > 0) {
            return AjaxResult.success("Customer added successfully");
        } else {
            return AjaxResult.error("Failed to add customer");
        }
    }

    // 更新客户信息
    @PutMapping("/{customerid}")
    @ApiOperation(value = "Update customer information", notes = "Update details of an existing customer by their ID. The customer ID in the URL should match the ID of the customer being updated.")
    public AjaxResult updateCustomer(@PathVariable Integer customerid, @Valid @RequestBody Customer customer) {
        customer.setCustomerid(customerid); // 更新指定ID的客户信息
        int result = customerService.updateByPrimaryKey(customer);
        if (result > 0) {
            return AjaxResult.success("Customer updated successfully");
        } else {
            return AjaxResult.error("Failed to update customer");
        }
    }

    // 删除客户
    @DeleteMapping("/{customerid}")
    @ApiOperation(value = "Delete customer", notes = "Delete a customer from the system by their ID. The customer ID should be provided in the URL to delete the specific record.")
    public AjaxResult deleteCustomer(@PathVariable Integer customerid) {
        int result = customerService.deleteByPrimaryKey(customerid);
        if (result > 0) {
            return AjaxResult.success("Customer deleted successfully");
        } else {
            return AjaxResult.error("Failed to delete customer");
        }
    }
}
